// todo 仅 菜单智能推送 下有引用
import apis from "../services/AddSourceApi";

const { targetQueryByName, targetQueryByOrg } = apis;
const initState = {
  loading: false,
  pageNum: 1,
  pageSize: 5,
  datas: [],
  content: " ",
  orgCode: "",
  totalCount: 0,
  totalPage: 0,
  query: "name"
};

export default {
  namespace: "targetPersonSelectModel",
  state: initState,
  effects: {
    *queryByName({ payload }, { call, put }) {
      yield put({ type: "triggerLoading", payload: true });
      const { datas = [] } = yield call(targetQueryByName, {
        content: payload || " "
      });

      yield put({
        type: "saveData",
        payload: { datas, totalCount: datas.length }
      });
      yield put({ type: "triggerLoading", payload: false });
      yield put({ type: "changeQuery", payload: "name" });
      yield put({
        type: "saveFilter",
        payload: { content: payload || "", pageNum: 1 }
      });
    },
    *queryByOrg({ payload }, { call, put }) {
      yield put({ type: "triggerLoading", payload: true });
      const { datas = [], totalPage = 0, totalCount = 0 } = yield call(
        targetQueryByOrg,
        { orgCode: payload }
      );
      yield put({
        type: "saveData",
        payload: { datas, totalPage, totalCount }
      });
      yield put({ type: "triggerLoading", payload: false });
      yield put({ type: "changeQuery", payload: "org" });
      yield put({
        type: "saveFilter",
        payload: { orgCode: payload || "", pageNum: 1 }
      });
    },
    *query({ payload }, { call, put, select }) {
      const queryWay = yield select(
        ({ targetPersonSelectModel: { query } }) => query
      );
      let params;
      let resData;
      yield put({ type: "triggerLoading", payload: true });
      if (queryWay === "name") {
        const contentCache = yield select(
          ({ targetPersonSelectModel: { content } }) => content
        );
        params = { content: contentCache };
        resData = yield call(targetQueryByName, params);
        const { datas } = resData;
        yield put({
          type: "saveData",
          payload: { datas, totalCount: datas.length }
        });
      } else {
        const orgCodeCache = yield select(
          ({ targetPersonSelectModel: { orgCode } }) => orgCode
        );
        params = { orgCode: orgCodeCache, pageNum: payload };
        resData = yield call(targetQueryByOrg, params);
        const { datas, totalPage, totalCount } = resData;
        yield put({
          type: "saveData",
          payload: { datas, totalPage, totalCount }
        });
      }
      yield put({ type: "triggerLoading", payload: false });
      yield put({ type: "saveFilter", payload: { pageNum: payload } });
    }
  },
  reducers: {
    saveData: (state, { payload }) => ({ ...state, ...payload }),
    saveFilter: (state, { payload }) => ({ ...state, ...payload }),
    changeQuery: (state, { payload }) => ({ ...state, query: payload }),
    triggerLoading: (state, { payload }) => ({ ...state, loading: payload }),
    reset: (state, { payload }) => ({
      ...state,
      ...payload,
      ...initState
    }),
    clearData: () => ({ ...initState })
  }
};
